#include "SegImageWrapper.h"
#include "SimpleAutomaticSegmentation.h"

#include "itkImageRegionConstIterator.h"
#include "itkWatershedImageFilter.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkImageToVTKImageFilter.h"
#include "itkSmartPointer.h"
#include "itkMinimumMaximumImageCalculator.h"
#include "itkConnectedThresholdImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkSmartPointer.h"
#include "itkPoint.h"

#include <QFile>
#include <QIODevice>
#include <QTextStream>
#include <QVector>
#include <QMessageBox>
#include <QString>
#include <QColor>
#include <QPointF>
//#include "qwt_spline.h"
//#include "qwt_plot_curve.h"
//#include "qwt_plot.h"
//#include "qwt_symbol.h"
//#include <qwt_curve_fitter.h>


SimpleAutomaticSegmentation::SimpleAutomaticSegmentation(Material *material)
{
    m_Material = material;

    m_ConnectedFilter = ConnectedFilterType::New();
    m_BinaryToLabelFilter = BinaryToLabelFilterType::New();
}

SimpleAutomaticSegmentation::~SimpleAutomaticSegmentation()
{
   //
}

void
SimpleAutomaticSegmentation::Initialize(GreyImagePointer image, double x, double y, double z)
{
    QMessageBox msgBox2;
    msgBox2.setText(QString::number(x)+" "+QString::number(y)+
                   " "+QString::number(z));
        msgBox2.exec();
    m_InputImage = image;
    m_WorldSeed[0] = x;
    m_WorldSeed[1] = y;
    m_WorldSeed[2] = z;

    GreyImageType::IndexType index;
    m_InputImage->TransformPhysicalPointToIndex(m_WorldSeed, m_IndexSeed);


   // QMessageBox msgBox;
   // msgBox.setText(QString::number(m_InputImage->GetPixel(index)));
   // msgBox.exec();

   /* QMessageBox msgBox;
    msgBox.setText(QString::number(m_Seed[0])+" "+QString::number(m_Seed[1])+
                   " "+QString::number(m_Seed[2]));
        msgBox.exec();*/

    this->DoSegmentation();
    m_Initialized = true;
}

GreyImageType&
SimpleAutomaticSegmentation::DoSegmentation()
{
    m_ConnectedFilter->SetReplaceValue(m_Material->GetLabel());
    m_ConnectedFilter->SetLower(m_Material->GetValue()[0]);
    m_ConnectedFilter->SetUpper(m_Material->GetValue()[1]);
    m_ConnectedFilter->SetSeed(m_IndexSeed);
    m_ConnectedFilter->SetInput(m_InputImage);
    m_ConnectedFilter->Update();

    m_BinaryToLabelFilter->SetInputForegroundValue(m_Material->GetLabel());
    m_BinaryToLabelFilter->SetInput(m_ConnectedFilter->GetOutput());
    m_BinaryToLabelFilter->Update();

    m_LabelMap = m_BinaryToLabelFilter->GetOutput();

  //  QMessageBox msgBox;
  //  msgBox.setText(QString::number(m_BinaryToLabelFilter->GetOutput()->GetNumberOfLabelObjects()));
 //   msgBox.exec();
    if (m_BinaryToLabelFilter->GetOutput()->GetNumberOfLabelObjects() == 0)
    {
        QMessageBox::critical(0, "Amaranthus", "Error! Nothing has been segmented. "
                                                         "Please, try again.");
        m_LabelMap = NULL;
    }
    else
        for(int i = 0; i < m_BinaryToLabelFilter->GetOutput()->GetNumberOfLabelObjects(); i++)
        {
            LabelObjectType* labelObject = m_LabelMap->GetNthLabelObject(i);
            labelObject->SetLabel(m_Material->GetLabel());
        }
}
